package com.lib.utls.internetime;

import android.util.Log;

import java.util.Calendar;
import java.util.TimeZone;

public class InterneTimeUtil {

    /*
     * 使用Calendar获取系统时间
     */

    public static String getCalendarTime() {
        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        int mYear = c.get(Calendar.YEAR);//年
        int mMonth = c.get(Calendar.MONTH) + 1;//月
        int mDay = c.get(Calendar.DAY_OF_MONTH);//日
        int mHour = c.get(Calendar.HOUR_OF_DAY);//24小时格式    HOUR(12小时格式)
        int mMinute = c.get(Calendar.MINUTE);//分
//        int mSecond = c.get(Calendar.SECOND);//秒
//        String time = mYear + "-" + getStrs(mMonth) + "-" + getStrs(mDay) + " " +
//                getStrs(mHour) + ":" + getStrs(mMinute) + ":" + getStrs(mSecond);

        String time = mYear + "-" + getStrs(mMonth) + "-" + getStrs(mDay) + " " +
                getStrs(mHour) + ":" + getStrs(mMinute);
        Log.e("测试网络时间", time);

        return time;
    }

    /**
     * 个位数的时候前面补一个0
     *
     * @param num
     * @return
     */
    public static String getStrs(int num) {
        String string;
        if (isNum(num)) {
            string = String.valueOf(num);
        } else {
            string = "0" + num;
        }
        return string;
    }

    /**
     * 判断是否为个位数
     *
     * @param num
     * @return
     */
    public static boolean isNum(int num) {
        boolean isNum;
        if (num > 9) {
            isNum = true;
        } else {
            isNum = false;
        }
        return isNum;
    }


}
